// zh_cjdianc/order/changeOrder.js
var a = getApp(), util = require("../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consume: 0,
    orderList:[],
    popErrorMsg:'',
    page:1,
  },
  consume_0: function() {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    a.util.request({
      url: "entry/wxapp/newuserorder",
      cachetime: "0",
      data: { user_id: user_id, status: 1 },
      success: function (t) {
        n.setData({
          orderList: t.data.data.list,
          consume: 0,
          page:1
        });
      }
    });
  },
  consume_1: function() {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    a.util.request({
      url: "entry/wxapp/newuserorder",
      cachetime: "0",
      data: { user_id: user_id, status: 2 },
      success: function (t) {
        n.setData({
          orderList: t.data.data.list,
          consume: 1,
          page: 1
        });
      }
    });
  },
  // 删除订单
  delOrder: function(e) {
    var n = this;
    var list = n.data.orderList;
    console.log(list);
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success: function(res) {
        if (res.cancel) {}
        else{
        var user_id = wx.getStorageSync('users').id;
        console.log(e);
        var order_id = e.target.dataset.order_id;
        var index = e.target.dataset.index;
        console.log(index,123);
        a.util.request({
          url: "entry/wxapp/newuserorderdel",
          cachetime: "0",
          data: { user_id: user_id, order_id: order_id },
          success: function (t) {
            console.log(t);
            if (t.data.code == 0) {
              // var list = n.data.orderList;
              // console.log(list);
              // list = list.splice(index, 1);
              wx.showToast({
                title: "成功",
                icon: 'success',//图标，支持"success"、"loading"
                success: function () { 
                  // var list = n.data.orderList;
                  var user_id = wx.getStorageSync('users').id;
                  a.util.request({
                    url: "entry/wxapp/newuserorder",
                    cachetime: "0",
                    data: { user_id: user_id, status: 2 },
                    success: function (t) {
                      n.setData({
                        orderList: t.data.data.list,
                        page:1
                      });
                    }
                  });
                },
              });
             
            } else {
              n.setData(
                { popErrorMsg: t.data.message }
              )
              n.ohShitfadeOut(); return;
            }
          }
        });
        }
      }
    })
  },
  // 修改订单
  changeOrder:function(e){
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    console.log(e);
    var order_id = e.target.dataset.order_id;
    a.util.request({
      url: "entry/wxapp/newuserorderupdate",
      cachetime: "0",
      data: { user_id: user_id, order_id: order_id },
      success: function (t) {
        console.log(t);
        if(t.data.code == 0){
          wx.navigateTo({
            url: 'reserve?order_id=' + e.currentTarget.dataset.order_id,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }else{
          n.setData(
            { popErrorMsg: t.data.message }
          )
          n.ohShitfadeOut(); return;
        }
      }
    });
  },
  // 评价
  tappj: function (e) {
    var n = this;
    console.log(e);
    var order_id = e.target.dataset.order_id;
    wx.navigateTo({
      url: '../pages/wddd/pjdd?order_id=' + e.currentTarget.dataset.order_id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    });
  },
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var n = this;
    a.setNavigationBarColor(this);
    var user_id = wx.getStorageSync('users').id;
    var status=options.status;
    if (!status){
      status = 1;
    }
    a.util.request({
      url: "entry/wxapp/newuserorder",
      cachetime: "0",
      data: { user_id: user_id,status:status},
      success: function (t) {
        n.setData({
          orderList: t.data.data.list,
          page:1
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    var consume = n.data.consume;
    var status;
    if (consume == 0) {
      status = 1;
    } else {
      status = 2;
    }
    a.util.request({
      url: "entry/wxapp/newuserorder",
      cachetime: "0",
      data: { user_id: user_id, status: status },
      success: function (t) {
        n.setData({
          orderList: t.data.data.list,
          page: 1
        });
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }
    });
  },
  onReachBottom: function () {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    var page = n.data.page;
    page = page + 1;
    var consume = n.data.consume;
    var status;
    if (consume == 0) {
      status = 1;
    } else {
      status = 2;
    }
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    a.util.request({
      url: "entry/wxapp/newuserorder",
      cachetime: "0",
      data: { user_id: user_id, status: status, page: page },
      success: function (t) {
        // 回调函数
        var orderList = n.data.orderList;

        for (var i = 0; i < t.data.data.list.length; i++) {
          orderList.push(t.data.data.list[i]);
        }
        // 设置数据
        n.setData({
          orderList: orderList,
          page: page
        });
        // 隐藏加载框
        wx.hideLoading();
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})